<template>
    <div class="articleList">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-tickets"></i> 文章列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="handleAdd">发布新文章</el-button>
                <!-- <el-select v-model="select_cate" placeholder="原创/转载" class="handle-select mr10">
                    <el-option key="1" label="原创" value="原创"></el-option>
                    <el-option key="2" label="转载" value="转载"></el-option>
                </el-select> -->
                <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
            </div>
            <el-table v-loading="loading"  element-loading-text="拼命加载中" :data="data.records" border style="width: 100%" ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="createTime" label="日期" sortable ></el-table-column>
                <el-table-column prop="articleClassName" label="分类"></el-table-column>
                <el-table-column prop="title" label="标题" ></el-table-column>
                <el-table-column prop="author" label="作者" ></el-table-column>
                <el-table-column label="原创/转载" >
                    <template  slot-scope="scope">
                        <el-tag v-if="scope.row.isOriginal" type="success">原创</el-tag>
                        <el-tag v-else type="warning" >转载</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="updateTime" label="最后编辑日期" sortable></el-table-column>
                <el-table-column
                    prop="tag"
                    label="标签"
                    width="200"
                    filter-placement="bottom-end">
                    <template  slot-scope="scope">
                        <el-tag v-if="scope.row.tag">{{scope.row.tag}}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" >
                    <template slot-scope="scope">
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                :page-size="articleList.size"
                :page-count="articleList.pages"
                layout="prev, pager, next"
                :total="articleList.total"
                @current-change="handleCurrentChange">
              </el-pagination>
             
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="80%" :before-close="closeEdit" :top='"2vh"' >
            <ArticleSave v-if="hackReset" :saveType='saveType' :enTitle='tempenTitle' @saveSuccess="saveSuccess"></ArticleSave>
        </el-dialog>

        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import ArticleSave from './ArticleSave.vue'
    export default {
        name: 'articleList',
        components: { ArticleSave },
        data() {
            return {
                loading: true,
                url: '/article/list',
                // url: './static/vuetable.json',
                tempenTitle: '',
                hackReset: false,
                saveType: '',
                articleList: [],
                cur_page: 1,
                page_size: 10,
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                is_search: false,
                editVisible: false,
                delVisible: false,
                form: {
                    name: '',
                    date: '',
                    address: ''
                },
                idx: -1
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
                return this.articleList
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },
            // 获取 easy-mock 的模拟数据
            getData() {
                // 开发环境使用 easy-mock 数据，正式环境使用 json 文件
                // if (process.env.NODE_ENV === 'development') {
                //     this.url = '/ms/table/list';
                // };
                this.loading =true
                this.$axios.post(this.url, {'size': this.page_size,'current': this.cur_page }).then((res) => {
                    this.articleList = res.data.model
                    this.loading =false
                }).catch(function(){
                    this.loading =false
                    this.$message.error('服务器异常');
                })
            },
            search() {
                this.is_search = true;
            },
            formatter(row, column) {
                return row.address;
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            handleEdit(index, row) {
                this.saveType = 'edit'
                this.hackReset =true
                this.tempenTitle = row.enTitle
                this.editVisible = true;
            },
            handleAdd() {
                this.saveType = 'add'
                this.hackReset =true
                this.editVisible = true
            },
            closeEdit(){
                this.hackReset = false //销毁组件
                this.editVisible = false
            },
            handleDelete(index, row) {
                this.idx = index;
                this.delVisible = true;
            },
            delAll() {
                const length = this.multipleSelection.length;
                let str = '';
                this.del_list = this.del_list.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                }
                this.$message.error('删除了' + str);
                this.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 保存编辑
            saveEdit() {
                this.$set(this.articleList, this.idx, this.form);
                this.editVisible = false;
                this.$message.success(`修改第 ${this.idx+1} 行成功`);
            },
            // 确定删除
            deleteRow(){
                let that = this
                let enTitle = that.articleList.records[(this.idx)].enTitle
                that.$axios.get('/article/del?enTitle=' + enTitle).then((res) => {
                   if(res.data.success){
                        that.articleList.splice(that.idx, 1);
                        that.$message.success('删除成功');
                    }else{
                        that.$message.error(res.data.message);
                    }
                })
                that.delVisible = false;
            },
            saveSuccess() {
                this.closeEdit()
                this.editVisible = false    
                this.getData()
            }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
</style>
